package com;

import com.sun.jna.Library;
import com.sun.jna.Native;

public class WindowsApi {
	public interface User32 extends Library {

		User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);

		int GetLogicalDriveStringsA(int length, byte[] buffer);

		int GetSystemDirectoryA(byte[] buffer, int size);

		int GetForegroundWindow();

		int GetWindowTextA(int x, byte[] x2, int x3);

	}

	public static String getForegroundWindow() {
		String os = System.getProperty("os.name");
		if (!os.toLowerCase().startsWith("win")) {
			return "Google Chrome";
		}
		User32 lib = User32.INSTANCE;
		int x = lib.GetForegroundWindow();
		byte[] windowText = new byte[512];
		lib.GetWindowTextA(x, windowText, 512);
		int i = 0;
		for (; i < windowText.length; i++) {
			if (windowText[i] == '\0')
				break;
		}
		byte[] windowText2 = new byte[i];
		i = 0;
		for (; i < windowText2.length; i++) {
			windowText2[i] = windowText[i];
		}
		return new String(windowText2);
	}

}
